package com.example.music_project.controller;

import com.example.music_project.mapper.MusicMapper;
import com.example.music_project.model.MusicInfo;
import com.example.music_project.model.User;
import com.example.music_project.service.MusicService;
import com.example.music_project.tools.Constant;
import com.example.music_project.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.ognl.Ognl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.objenesis.ObjenesisHelper;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/music")
public class MusicController {
    @Autowired
    MusicService musicService;

    //上传音乐文件
    @RequestMapping("/upMusicFile")
    public Result<MusicInfo> upMusicFile(MultipartFile filename, String singer, HttpServletRequest request, HttpServletResponse response) {
        log.info(singer);
        Result<MusicInfo> result = new Result<>();
        //校验参数
        if (!StringUtils.hasLength(singer) || filename == null) {
            log.error("参数错误");
            return result.fail("参数错误");
        }
        Result<MusicInfo> musicInfoResult = musicService.upFile(singer, filename, request, response);
        if (musicInfoResult.getCode() == 200) {
            try {
                response.sendRedirect("/list.html");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            log.info("code:{}", musicInfoResult.getCode() == 200);
        }
        return musicInfoResult;
    }
    //播放音乐
    @RequestMapping("/listen")
    public ResponseEntity<byte[]> listen(String path) {
        log.info("path:{}" , path);
//        return result.success(musicService.listenLogic(path));
        return musicService.listenLogic(path);
    }

    // 删除音乐
    @RequestMapping("/delete")
    public Result<Object> delete(Integer id) {
        return musicService.deleteLogic(id);
    }

    // 批量删除
    @RequestMapping("/deleteAll")
    public Result<Object> deleteAll(@RequestParam("id[]") List<Integer> id) {
        log.info("id:{}", id.toString());
        return musicService.deleteAllLogic(id);
    }

    //查询音乐,音乐列表
    @RequestMapping("/queryMusic")
    public Result<List<MusicInfo>> queryMusic(String musicName) {
        List<MusicInfo> musicInfos = musicService.queryMusicLogic(musicName);
        Result<List<MusicInfo>> result = new Result<>();
        return result.success(musicInfos);
    }

    //收藏音乐
    @RequestMapping("collect")
    public Result<Object> collectMusic(Integer id, HttpServletRequest request) {
        log.info("id:{}", id);
        return musicService.collectLogic(id, request)  ;
    }

    //查询收藏音乐
    @RequestMapping("/queryCollect")
    public Result<Object> queryForCollect(String musicName, HttpServletRequest request) {
        log.info("musicName:{}", musicName);
        return musicService.queryForCollectLogic(musicName, request);
    }

    //取消音乐收藏
    @RequestMapping("/deleteCollect")
    public Result<Object> deleteCollect(Integer id, HttpServletRequest request) {
        log.info("music:{}", id);
        return musicService.deleteCollectLogic(id, request);
    }




}
